package com.weirdor.boot.admin.www.sys;

import com.weirdor.boot.admin.www.common.AbstractController;
import com.weirdor.boot.common.annotation.SysLog;
import com.weirdor.boot.common.utils.PageUtils;
import com.weirdor.boot.common.utils.R;
import com.weirdor.boot.common.utils.ValidatorUtils;
import com.weirdor.boot.common.validator.Assert;
import com.weirdor.boot.common.validator.group.AddGroup;
import com.weirdor.boot.common.validator.group.UpdateGroup;
import com.weirdor.boot.module.entity.SysUser;
import com.weirdor.boot.module.service.ISysUserRoleService;
import com.weirdor.boot.module.service.ISysUserService;
import com.weirdor.boot.module.utils.ShiroUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 系统用户
 */
@RestController
@RequestMapping("/api")
public class SysUserController extends AbstractController {
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysUserRoleService sysUserRoleService;

    /**
     * 所有用户列表
     */
    @RequestMapping("/sys/user/list")
    @RequiresPermissions("sys:user:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = sysUserService.queryPage(params);

        return R.ok().put("data", page.getList());
    }

    /**
     * 获取登录的用户信息
     */
    @RequestMapping("/sys/user/info")
    public R info() {
        return R.ok().put("user", getUser());
    }

    /**
     * 修改登录用户密码
     */
    @SysLog("修改密码")
    @RequestMapping("/sys/user/password")
    public R password(String oldPassword, String repassword) {
        Assert.isBlank(repassword, "新密码不为能空");

        //原密码
        oldPassword = ShiroUtils.sha256(oldPassword, getUser().getSalt());
        System.out.println(oldPassword);
        //新密码
        repassword = ShiroUtils.sha256(repassword, getUser().getSalt());

        //更新密码
        boolean flag = sysUserService.updatePassword(getUserId(), oldPassword, repassword);
        if (!flag) {
            return R.error("原密码不正确");
        }

        return R.ok();
    }

    /**
     * 用户信息
     */
    @RequestMapping("/sys/user/info/{userId}")
    @RequiresPermissions("sys:user:info")
    public R info(@PathVariable("userId") Long userId) {
        SysUser user = sysUserService.selectById(userId);

        //获取用户所属的角色列表
        List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
        user.setRoleIdList(roleIdList);

        return R.ok().put("user", user);
    }

    /**
     * 保存用户
     */
    @SysLog("保存用户")
    @RequestMapping("/sys/user/save")
    @RequiresPermissions("sys:user:save")
    public R save(@RequestBody SysUser user) {
        ValidatorUtils.validateEntity(user, AddGroup.class);

        sysUserService.save(user);

        return R.ok();
    }

    /**
     * 修改用户
     */
    @SysLog("修改用户")
    @RequestMapping("/sys/user/update")
    @RequiresPermissions("sys:user:update")
    public R update(@RequestBody SysUser user) {
        ValidatorUtils.validateEntity(user, UpdateGroup.class);

        sysUserService.update(user);

        return R.ok();
    }

    /**
     * 删除用户
     */
    @SysLog("删除用户")
    @RequestMapping("/sys/user/delete")
    @RequiresPermissions("sys:user:delete")
    public R delete(@RequestBody Long[] userIds) {
        if (ArrayUtils.contains(userIds, 1L)) {
            return R.error("系统管理员不能删除");
        }

        if (ArrayUtils.contains(userIds, getUserId())) {
            return R.error("当前用户不能删除");
        }

        sysUserService.deleteBatchIds(Arrays.asList(userIds));

        return R.ok();
    }
}
